export default function({tapDay, today, funButtons, record, caseKey}){
  const $$ = wx.calendarInst.getDateHandle
  const that = this
  const key = caseKey
  const util = this._

  funButtons.dot.unshift({
    aim: 'onRemind',
    itemClass: 'remind-case', 
    img: {src: '/images/remind.png'}
  })

  funButtons.dot = funButtons.dot.map(item=>{
    if (item.aim) {
      item.aim = item.aim + '?key='+key+'&type=commemorate'
    }
    return item
  })

  // if (record) {
  //   wx.showToast({
  //     title: '重复设置',
  //     icon: 'error',
  //     duration: 2000
  //   })
  //   return
  // }

  const daystamp = 24 * 60 * 60 * 1000
  const nextCommemorateDay = util.getYmd(`${tapDay.year+1}-${tapDay.month}-${tapDay.day}`)
  const remainder = (tapDay.timestamp - today.timestamp) % daystamp
  let   diffDays = (tapDay.timestamp - today.timestamp) / daystamp + remainder
  let   passed = false
  if (diffDays < 0) {
    passed = true
    diffDays = (nextCommemorateDay.timestamp - today.timestamp) / daystamp + remainder
  }

  tapDay.diffDays = diffDays
  const title = `${ (passed ? nextCommemorateDay.year : tapDay.year) }年${tapDay.month}月${tapDay.day}日`
  const content = {body: [`${diffDays}`, title], date: tapDay.timestr, itemClass: 'commemorate', targetDay: tapDay, ...funButtons}
  

  function beforeCallback({createCaseCard}){
    wx.modalx.showModal({
      content: {
        // title: ['倒数', diffDays+'', '天'],
        // title: [' ', diffDays+'', ' '],
        title: [' ', '🌹', ' '],
        body: [
          {
            title: [
              '纪念日：',
              {
                $$id: 'commemorate-reason-title',
                title: ' '
              }
            ], 
            titleStyle: 'display: grid; grid-template-columns: 1fr 3fr; grid-template-rows: 1fr;box-sizing: border-box; padding-left: 0.3em;',
            "@list":{
              type: {
                is: 'scroll',
                "scroll-y": true,
                "scroll-x": false,
              },
              data: [
                '自定义',
                '生日',
                '爸爸生日',
                '妈妈生日',
                '结婚纪念日'
              ],
              itemMethod: {
                tap(e, param, inst){
                  inst.siblings().removeClass('active', function(){
                    inst.addClass('active')
                    const titleInst = that.getElementsById('commemorate-reason-title')
                    const data = inst.getData()
                    const title = data.title
                    if (titleInst) {
                      if (title === '自定义') {
                        titleInst.update({title: '自定义，请在卡片中编辑'})
                        content.body[2] = '纪念一点什么'
                      } else {
                        titleInst.update({title})
                        content.body[2] = title
                      }
                    }
                  })
                }
              },
              listClass: 'commemorate-reason-list',
              itemClass: 'commemorate-reason-item',
            },
            dot: [
              {title: '纪念日为每年同一日期', itemStyle: 'height: 3em; font-size: 14px;'},
              {
                title: '我的纪念日', 
                itemClass: 'pop-case-scroll-bottom', 
                itemStyle: 'margin-top: 0;',
                aim(){
                  wx.modalx.closeModal()
                  that.restoreCaseButton()
                  wx.pageScrollTo({
                    selector: '#pits-list-container',
                    duration: 300,
                    success(){
                      that.listCaseType('commemorate')
                    }
                  })
                }
              }, 
            ]
          },
        ],
        itemClass: 'inner-content',
        bodyStyle: 'height: 350px;'
      },
      confirmText: '确定',
      cancelText: '取消', 
      height: 65,
      width: 80,
      itemClass: 'popup commemorate', 
      mask: false,
      success(res){
        if (res.confirm) {
          const timestr = tapDay.timestr
          const currentDay = $$(timestr)
          currentDay && currentDay.addClass('commemorate');
          if (!content.body[2]) {
            content.body[2] = '纪念一点什么'
          }
          createCaseCard(content);
        }
        that.restoreCaseButton()
      }
    })
  }

  return {key, content, beforeCallback}
}